<template>
	<a-drawer
		title="试剂导入"
		:width="1200"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
		:mask-closable="false"
	>
		<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
			<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState"
					class="ant-advanced-search-form">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="导入批次" name="impBat">
							<a-input v-model:value.trim="searchFormState.impBat" placeholder="请输入导入批次"></a-input>
						</a-form-item>
					</a-col>
					<a-col :span="16">
						<a-button type="primary" @click="table.refresh(true)">查询</a-button>
						<a-button style="margin: 0 8px" @click="resetForm()">重置</a-button>
					</a-col>
				</a-row>
			</a-form>
		</a-card>
		<a-card :bordered="false">
			<s-table
				ref="table"
				:columns="columns"
				:data="loadData"
				bordered
				:row-key="(record) => record.id"
			>
				<template #operator class="table-operator">
					<a-space>
						<a-button type="primary" ghost @click="exportTmpl()"
								  :loading="exportLoading">
							<VerticalAlignBottomOutlined/>模板下载
						</a-button>
						<a-upload name="file" :show-upload-list="false" :custom-request="uploadFile"
								  accept=".xls,.xlsx">
							<a-button type="primary" :loading="uploadLoading">
								<UploadOutlined/>导入
							</a-button>
						</a-upload>
					</a-space>
				</template>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'impBat'">
						<a-tag color="blue">{{record.impBat}}</a-tag>
					</template>
					<template v-if="column.dataIndex === 'totalCount'">
						<a-tag color="orange">{{record.totalCount}}</a-tag>
					</template>
					<template v-if="column.dataIndex === 'todoCount'">
						<a-tag>{{record.todoCount}}</a-tag>
					</template>
					<template v-if="column.dataIndex === 'errorCount'">
						<a-tag  color="red">{{record.errorCount}}</a-tag>
					</template>
					<template v-if="column.dataIndex === 'succCount'">
						<a-tag  color="green">{{record.succCount}}</a-tag>
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a @click="reagentImportItemRef.onOpen(record)">详情</a>
					</template>
				</template>
			</s-table>
		</a-card>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
		</template>
		<ReagentImportItem ref="reagentImportItemRef"/>
	</a-drawer>
</template>

<script setup name="bizReagentImpl">
	import bizGoodsImpApi from '@/api/biz/bizGoodsImpApi'
	import { cloneDeep } from "lodash-es";
	import { message } from 'ant-design-vue'
	import ReagentImportItem from './reagentImportItem.vue'

	// 抽屉状态
	const visible = ref(false)

	let searchFormState = reactive({
		goodsType: 1
	})
	const searchFormRef = ref()
	const reagentImportItemRef = ref()
	const table = ref()
	//导出loading
	const exportLoading = ref(false)
	//导入loading
	const uploadLoading = ref(false)

	const columns = [
		{
			title: '导入批次',
			dataIndex: 'impBat',
			align:'center'
		},
		{
			title: '总数量',
			dataIndex: 'totalCount',
			align:'center'
		},
		{
			title: '待处理数量',
			dataIndex: 'todoCount',
			align:'center'
		},
		{
			title: '成功数量',
			dataIndex: 'succCount',
			align:'center'
		},
		{
			title: '错误数量',
			dataIndex: 'errorCount',
			align:'center'
		},
		{
			title: '创建人',
			dataIndex: 'createUserName',
			align:'center'
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			align:'center'
		}
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	})

	const resetForm = () => {
		searchFormRef.value.resetFields()
	}

	// 打开抽屉
	const onOpen = () => {
		visible.value = true
	}

	const loadData = (parameter) => {
		return bizGoodsImpApi.bizGoodsImpPage(Object.assign(parameter, searchFormState)).then((data) => {
			return data
		})
	}

	// 关闭抽屉
	const onClose = () => {
		searchFormRef.value.resetFields()
		visible.value = false
	}

	const exportTmpl = () => {
		exportLoading.value = true
		bizGoodsImpApi.bizReagentImpExportTmpl().then((res) => {
			const blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' });
			const contentDisposition = res.headers['content-disposition']
			const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
			const $link = document.createElement("a");
			$link.href = URL.createObjectURL(blob);
			var reg = /^["](.*)["]$/g
			$link.download = decodeURI(patt.exec(contentDisposition)[1].replace(reg, '$1'))
			$link.click();
			document.body.appendChild($link);
			document.body.removeChild($link); // 下载完成移除元素
			window.URL.revokeObjectURL($link.href); // 释放掉blob对象
		}).finally(() => {
			exportLoading.value = false
		})
	}

	const uploadFile = (data) => {
		uploadLoading.value = true
		const fileData = new FormData()
		fileData.append('file', data.file)
		bizGoodsImpApi.bizReagentImport(fileData).then((data) => {
			message.success('导入成功！共导入[' + data + ']条数据,请关注后续解析情况！')
			table.value.refresh(true)
		}).finally(() => {
			uploadLoading.value = false
		})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
